<%--
  Created by IntelliJ IDEA.
  User: 15890741124
  Date: 2022/8/9
  Time: 14:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>详细信息</title>
    <script src="https://cdn.jsdelivr.net/npm/echarts@5.3.3/dist/echarts.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
</head>
<body>
<%--查询--%>
<form class="layui-form" action="" style="margin-top: 20px">
    <div class="layui-inline">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" placeholder="请输入姓名" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">测试计划ID</label>
        <div class="layui-input-block">
            <select placeholder="选择ID" id="cccc" name="testPlanId"></select>
        </div>
    </div>

    <div class="layui-inline">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">查询</button>
            <button class="layui-btn layui-btn-primary" id="reset" type="reset">重置</button>
        </div>
    </div>
</form>
<%--表格--%>
<table id="demo" lay-filter="test"></table>
<%--表格右侧标签--%>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="check">查看</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="checkQuestion">查看结果</a>
</script>

<from style="display: none" id="question-answer">
    欢迎查看<span id="name"></span>的答题
    <table id="demo2" lay-filter="test"></table>
</from>

<%--表格头标签--%>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="del">删除</button>
    </div>
</script>
<%--查看页面--%>
<div id="show" style="width: 100%;height: 100%; display: none;">
    <div >
        <h1 style="text-align:center;line-height: 60px;font-size: 30px;font-weight: 600;color: #7a2114;">测评报告预览</h1>
    </div>
    <div id="div1" style="background: rgba(87,144,255,.2);padding: 20px;text-align: center;margin-bottom: 20px"></div>
    <div style="width: 100%;height: 350px;" id="shopping"></div>
    <div style="background: rgba(87,144,255,.2);padding: 20px;margin-top: 20px;text-align: center"
         id="div2">
    </div>
    <div style="background: white;padding: 20px;margin-top: 20px; text-align: center" id="div3"></div>
</div>
</body>
<script>

    layui.use(['table', 'form', 'upload', 'laydate', 'jquery'], function () {
        var table = layui.table;
        var form = layui.form;
        var upload = layui.upload;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        table.render({
            elem: '#demo'
            , height: 550
            , url: '${pageContext.request.contextPath}/view/query.do'
            , dataType: 'json'
            , page: true //开启分页
            // ,toolbar:'default'
            , limits: [5, 10, 15, 20, 25, 30, 100, 200]//将每页几个写在这里，不想默认10个每页
            , autoSort: true//关闭自动排序
            , cols: [[
                {checkbox: 'checkbox', fixed: 'left'}//左侧复选框
                , {field: 'testerId', title: 'ID', width: 120, sort: true}
                , {field: 'name', title: '姓名', width: 120}
                , {field: 'phone', title: '电话', width: 120}
                , {field: 'createTime', title: '测试时间', width: 120}
                , {field: 'redCount', title: '红色得分', width: 120 }
                , {field: 'yellowCount', title: '黄色得分', width: 120}
                , {field: 'blueCount', title: '蓝色得分', width: 120}
                , {field: 'greenCount', title: '绿色得分', width: 120}
                , {field: 'testPlanId', title: '测试计划ID', width: 120, hide: true}
                , {field: 'testName', title: '测试计划名称', width: 120}
                //右侧操作栏
                , {fixed: 'right', title: '操作', width: 200, align: 'center', toolbar: '#barDemo'}
            ]]
            , toolbar: "#toolbarDemo"//头部操作栏
        });



        //触发头部工具栏
        table.on('toolbar(test)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id)
                , data = checkStatus.data; //获取选中的数据

            switch (obj.event) {
                case "del":
                    if (data.length === 0) {
                        layer.msg('请选择一行');
                    } else {
                        //定义一个数组，封装所有的id
                        var ids = [];
                        //循环所有选中的行
                        for (let i = 0; i < data.length; i++) {
                            ids[i] = data[i].testerId;
                        }
                        layer.confirm('真的删除行么', function (index) {
                            $.ajax({
                                url: "${pageContext.request.contextPath}/view/delete.do",
                                data: {testerId: ids.join(",")},
                                //响应的数据类型
                                dataType: "JSON",
                                type: "POST"
                                //异步请求
                                // async: true,
                                //成功的回调
                                , success: function (data) {
                                    if (data.code == 0) {
                                        layer.closeAll();//关闭所有
                                        //刷新表格数据
                                        table.reload('demo', {
                                            page: {
                                                curr: 1
                                                //重新从第 1 页开始
                                            }
                                        });

                                    }
                                    layer.msg(data.msg);
                                }
                            });

                        });
                    }
                    break;
                default:
                    break;
            }
        });
        //监听右侧工具栏事件
        table.on('tool(test)', function (obj) {
            //当前页数据
            var demo = table.cache["demo"];
            var data = obj.data;
            switch (obj.event) {
                case 'check':
                    layer.open({
                        title: '性格测试结果图示',
                        type: 1,
                        content: $('#show'), //这里content是一个DOM，注意：最好该元素要存放在body最外层，否则可能被其它的相对元素所影响
                        area: ['571px', '700px']
                    });
                    var red = data.redCount
                    var blue = data.blueCount
                    var yellow = data.yellowCount
                    var green = data.greenCount
                    var myChart = echarts.init(document.getElementById('shopping'));
                    var option;
                    option = {
                        title: {
                            text: 'FPA性格测试',
                            left: 'center'
                        },
                        tooltip: {
                            trigger: 'item'
                        },
                        legend: {
                            orient: 'vertical',
                            left: 'left'
                        },
                        series: [
                            {
                                name: 'Fpa性格占比',
                                type: 'pie',
                                radius: '70%',
                                data: [
                                    {value: red, itemStyle: {color: '#c51313'}, name: "红色性格"},
                                    {value: blue, itemStyle: {color: '#131cc5'}, name: '蓝色性格'},
                                    {value: yellow, itemStyle: {color: '#b9c513'}, name: '黄色性格'},
                                    {value: green, itemStyle: {color: '#13c57b'}, name: '绿色性格'},
                                ],
                            }
                        ],
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    };
                    // 使用刚指定的配置项和数据显示图表。
                    myChart.setOption(option);
                    var a = [blue, red, yellow, green];
                function f(a, b) {  //排序函数
                    return (a - b);  //返回比较参数
                }
                    a = a.sort(f)
                    if (a[a.length - 1] == yellow) {
                        color = 1
                    } else if (a[a.length - 1] == red) {
                        color = 2
                    } else if (a[a.length - 1] == blue) {
                        color = 3
                    } else {
                        color = 4
                    }
                    $.ajax({
                        url: "${pageContext.request.contextPath}/view/div.do",
                        type: "POST",
                        dataType: "JSON",
                        data: {id: color},
                        success: function (ret) {
                            $('#div1').html(ret.data[0].div1)
                            $('#div2').html(ret.data[0].div2)
                            $('#div3').html(ret.data[0].div3)
                        }
                    });
                    break;
                    case "checkQuestion":
                        $("#username").html(data.name);
                        table.render({
                            elem: '#demo2'
                            , height: 550
                            , url: '${pageContext.request.contextPath}/view/queryQuestionFPA.do?id='+data.testerId
                            , dataType: 'json'
                            , page: true //开启分页
                            , limit: 10
                            , limits: [5, 10,20,50,100]
                            , autoSort: true//关闭自动排序
                            , cols: [[
                                {checkbox: 'checkbox', fixed: 'left'}//左侧复选框
                                , {field: 'id', title: '问题id', width: 100, sort: true, fixed: 'left'}
                                , {field: 'question', title: '问题', width: 100}
                                , {field: 'result', title: '答案', width: 150, sort: true}
                                , {field: 'dictionary_name', title: '题目类型', width: 150, sort: true}
                            ]]
                        });
                        layer.open({//弹出修改框
                            title: '修改',
                            type: 1,
                            content: $("#question-answer"),
                            area: ('600px', '600px')
                        });
                        break;
                default:
                    break;
            }
        });


        /*---------------------------查询--------------------------*/
        $.ajax({
            url: "${pageContext.request.contextPath}/view/pullFpa.do",
            type: "POST",
            dataType: "JSON",
            success: function (ret) {
                if (ret.code == 0) {
                    var htmlStr = "<option value=''>请选择ID</option>";
                    for (let i = 0; i < ret.data.length; i++) {
                        htmlStr += "<option value=" + ret.data[i].id + ">测试ID：" + ret.data[i].id + "</option>"
                    }
                    $("#cccc").html(htmlStr);
                    //重新渲染表单
                    layui.use('form', function () {
                        form.render('select');
                    })
                }
            }
        });
        //查询问题
        form.on('submit(formDemo)', function (data) {
            table.reload('demo', {
                where: {
                    //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    name: data.field.name //排序字段-->查询问题
                    , testPlanId: data.field.testPlanId//排序方式 --> 查询创建人
                }
                , page: {
                    curr: 1
                    //重新从第 1 页开始
                },
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        //重置查询条件
        $("#reset").click(function () {
            table.reload('demo', {
                where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
                    testName: "" //第一个查询条件
                    , createBy: ""//第二个查询条件
                },
                page: {
                    curr: 1
                    //重新从第 1 页开始
                },
            });
        });

    });

</script>
</html>
